package be.phl.layoutAdapter;

import java.util.List;
import java.util.ListIterator;

import com.devsmart.android.ui.HorizontalListView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import be.phl.model.DBAdapterScreening;
import be.phl.obstacleapp.R;

public class ItemProblemRowAdapter extends ArrayAdapter<ItemProblemRow> {

	private final Context context;
	private final List<ItemProblemRow> ItemProblems;
	private static final String TAG = "ItemProblemRowAdapter";
	
private int width, height;
	
	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height; 
	}

	public ItemProblemRowAdapter(Context context,
			List<ItemProblemRow> ItemProblems) {
		super(context, R.layout.item_problem_row, ItemProblems);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.ItemProblems = ItemProblems;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		getScreenDimensions();
		int textsize = getWidth() * 7 / 400;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View rowView = inflater.inflate(R.layout.item_problem_row,
				parent, false);

		TextView name = (TextView) rowView.findViewById(R.id.textView1);
		TextView selector = (TextView) rowView.findViewById(R.id.textView2);
		
		final ListView parentListview = (ListView) parent;

		final ItemProblemRow itemProblem = ItemProblems.get(position);
		Typeface tf = Typeface.createFromAsset(context.getAssets(), "com.ttf");
		name.setTypeface(tf);
		name.setText(itemProblem.getName());
		name.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		if (itemProblem.isBranchEnd()) {
			name.setTypeface(Typeface.DEFAULT_BOLD);
			rowView.setBackgroundResource(R.drawable.transition);

			rowView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int shortAnimTime = context.getResources().getInteger(
							android.R.integer.config_shortAnimTime);
					itemProblem.setChecked(!itemProblem.isChecked());

					// animate the color change
					TransitionDrawable transition = (TransitionDrawable) rowView
							.getBackground();
					if (itemProblem.isChecked()) {
						transition.startTransition(shortAnimTime);
					} else {
						transition.reverseTransition(shortAnimTime);
					}

					// save change to database
					DBAdapterScreening db = new DBAdapterScreening(context);
					db.open();
					if (itemProblem.isChecked()) {
						ItemProblems.get(position).setScreeningProblemId(
								(int) db.insertScreeningProblem(
										itemProblem.getScreeningItemId(),
										itemProblem.getId(), null));
						Log.i(TAG,
								"ScreeningProblemId: "
										+ ItemProblems.get(position)
												.getScreeningProblemId());
					} else {
						db.deleteScreeningProblem(itemProblem
								.getScreeningProblemId());
						Log.i(TAG, "deleted");
					}
					db.close();

//					// if the problem is singleselection set the other to not
//					// selected
//					if (itemProblem.isSingleSelection()
//							&& itemProblem.isChecked()) {
//						Log.i(TAG, "SingleSelection");
//						setUnselected(itemProblem);
//						notifyDataSetChanged();
//					}
				}

			});

			if (itemProblem.isChecked()) {
				TransitionDrawable transition = (TransitionDrawable) rowView
						.getBackground();
				transition.startTransition(0);
			}
		} else {
			if (itemProblem.isSelected())
				rowView.setBackgroundColor(Color.rgb(195, 195, 195));
			selector.setVisibility(itemProblem.isSelected() ? View.VISIBLE
					: View.GONE);
		}

		return rowView;
	}
	
	@SuppressLint("NewApi")
	public void getScreenDimensions() {
//
		if (android.os.Build.VERSION.SDK_INT >= 13) {

			Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
			Point size = new Point();
			display.getSize(size);
			width = size.x;
			height = size.y;
			Log.w(TAG,"width height "+width+" "+height);
		} else {
			Display display = ((Activity)context).getWindowManager().getDefaultDisplay();

			width = display.getWidth(); // deprecated
			height = display.getHeight(); // deprecated
		}

	}

	
//	 public void unSelectAll(ItemProblemRow row) { for (ItemProblemRow i :
//	 datalist) { if (i.getParentId() == row.getId() && i.isBranchEnd()) {
//	 i.setChecked(false); DBAdapterScreening db = new
//	 DBAdapterScreening(activity); db.open();
//	 db.deleteScreeningProblem(i.getScreeningProblemId()); db.close(); } }
	 

}
